package com.balloon.springboot.datasource.conf;

import com.balloon.springboot.datasource.prorperties.Conf;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

public class HikariDataSourceBuilder {
    private DataSourceProperties properties;
    private Conf conf;

    public DataSourceProperties getProperties() {
        return properties;
    }

    public void setProperties(DataSourceProperties properties) {
        this.properties = properties;
    }

    public Conf getConf() {
        return conf;
    }

    public void setConf(Conf conf) {
        this.conf = conf;
    }

    public static Builder builder() {
        return new Builder();
    }

    HikariDataSourceBuilder(Builder builder) {
        this.properties = builder.properties;
        this.conf = builder.conf;
    }

    public static class Builder {
        private DataSourceProperties properties;
        private Conf conf;


        public Builder setDataSourceProperties(DataSourceProperties properties) {
            this.properties = properties;
            return this;
        }

        public Builder setAdvancedAttributes(Conf conf) {
            this.conf = conf;
            return this;
        }

        public HikariDataSource build() {
            HikariDataSource hikariDataSource = new HikariDataSource();
            hikariDataSource.setUsername(properties.getUsername());
            hikariDataSource.setPassword(properties.getPassword());
            hikariDataSource.setJdbcUrl(properties.getUrl());
            if (conf != null) {
                if(conf.getMinldle()!=null){
                    hikariDataSource.setMinimumIdle(conf.getMinldle());
                }
                if(conf.getMaxPoolSize()!=null){
                    hikariDataSource.setMaximumPoolSize(conf.getMaxPoolSize());
                }
                if(conf.getMaxLifeTime()!=null){
                    hikariDataSource.setMaxLifetime(conf.getMaxLifeTime());
                }
                if(conf.getConnectionTimeout()!=null){
                    hikariDataSource.setConnectionTimeout(conf.getConnectionTimeout());
                }
            }
            return hikariDataSource;
        }
    }
}
